﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;


public class AvatarAnimation
{
    public AvatarHandle Handle;
    public ActionBase CurrentAction;
    private Animation animation;
    private Transform transform;
    public AvatarAnimation(AvatarHandle handle)
    {
        Handle = handle;
        animation = handle.Root.animation;
        transform = handle.Root.transform;
        animation["Daiji"].wrapMode = WrapMode.Loop;
        animation["Kuaipao"].wrapMode = WrapMode.Loop; 
        animation["Kuaipao"].speed = .8f;
        animation["Zoulu"].wrapMode = WrapMode.Loop;
        animation["Zoulu"].speed = 1.3f;

        animation["Tiao"].wrapMode = WrapMode.Once;
        animation["Tiao"].layer = 1;
        animation["Fangyu"].wrapMode = WrapMode.Loop;
        animation["Chongcigongji"].wrapMode = WrapMode.Once;
        animation["Chongcigongji"].layer = 1;

        animation["Kongzhonggongji"].wrapMode = WrapMode.Once;
        animation["Kongzhonggongji"].layer = 1;
        animation["Shoushang"].wrapMode = WrapMode.Once;
        animation["Shoushang"].speed = .5f;
        animation["Shoushang"].layer = 2;
        animation.Stop();
    }
    private string lastAnimationName = string.Empty;
    public void PlayAnimation(bool check, string animationName)
    {
        if (check && string.Compare(lastAnimationName, animationName) != 0)
        {
            lastAnimationName = animationName;
            //Debug.Log("animationName:" + animationName);
            animation.CrossFade(animationName, animation[animationName].length > .3f ? .3f : animation[animationName].length / 2.0f);
        }
    }
    public void PlayActionAnimation(ActionBase action)
    {
        if (string.Compare(lastCrossFadeQueuedAnimation, action.Parameter.AnimationName) != 0)
        {
            lastCrossFadeQueuedAnimation = CurrentAction.Parameter.AnimationName;
            lastAnimationName = lastCrossFadeQueuedAnimation;
            if (animation.IsPlaying(lastCrossFadeQueuedAnimation)) animation.Stop(lastCrossFadeQueuedAnimation);
            action.Animation = animation[lastAnimationName];
            action.Start();
            //animation.CrossFade(lastCrossFadeQueuedAnimation, 0.3f);
            animation.CrossFade(lastCrossFadeQueuedAnimation, 0.3f);
        }
    }
    public void OnBeat()
    {
        //animation.Play("Shoushang", AnimationPlayMode.Mix);
        PlayAnimation(Handle.InputHandle.ActionIdle, "Shoushang");
    }
    public void StartIdle()
    {
        PlayAnimation(Handle.InputHandle.ActionIdle, "Daiji");
    }
    public void StartWalk()
    {
        PlayAnimation(Handle.InputHandle.ActionIdle, "Zoulu");
    }
    public void StartRun()
    {
        PlayAnimation(Handle.InputHandle.ActionIdle, "Kuaipao");
    }
    public void StartJump()
    {
        PlayAnimation(Handle.InputHandle.ActionIdle, "Tiao");
    }
    public void StopJump()
    {
        if (Handle.Root.animation.IsPlaying("Tiao")) Handle.Root.animation.Stop("Tiao");
    }
    public void Update()
    {
        if (actionAnimationTasks.Count > 0)
        {
            Handle.InputHandle.ActionIdle = false;
            animationDuration += InputManager.DeltaTime;
            if(CurrentAction == null) CurrentAction = actionAnimationTasks[0];
            if (animationDuration > animation[CurrentAction.Parameter.AnimationName].length)
            {
                animationDuration = 0;
                lastCrossFadeQueuedAnimation = string.Empty;
                actionAnimationTasks.RemoveAt(0);
                CurrentAction = null;
            }
            else
            {
                PlayActionAnimation(CurrentAction);
            }
        }
        else
        {
            Handle.InputHandle.ActionIdle = true;
            Handle.Action.CanMultipleAction = true;
            animationDuration = 0;
        }
    }
    private string lastCrossFadeQueuedAnimation = string.Empty;
    private List<ActionBase> actionAnimationTasks = new List<ActionBase>();
    private ActionBase tmpActionBase;
    private float animationDuration = 0;
    public void ActionAnimation(ActionBase item)
    {
        actionAnimationTasks.Add(item);
    }
    public bool ExsitAction(UInt16 id)
    {
        foreach (ActionBase action in actionAnimationTasks)
        {
            if (id == action.Id)
            {
                return true;
            }
        }
        return false;
    }
}

